/*
wScrollToTop 功能是在页面右侧生成div,点击后使页面滚动到顶部
编写：邬畏畏
---------------------------
调用说明：
--------
在页面load时候调用 wScrollToTop.init()：
wScrollToTop.init();
wScrollToTop.init({fade:true,autoHide:true,right:"30px",content:"<div class='zdyScrollToTop'>▲</div>"});

功能说明：
在wScrollToTop的init函数里参数可有可无
无参数：
右侧自动生成浮动div,含有默认文本内容“置顶”,在整个页面中永远显示

有参数：
init()函数里是一个JSON数据
格式是{fade:true,autoHide:true,top:"40%",right:"30px",content:"<div class='zdyScrollToTop'>▲</div>"}
每个参数值都是可选的
fade:表示是否淡入淡出
autoHide:表示滚动到顶部时是否隐藏
top:表示距离页面顶部距离
right:表示距离右侧距离
content:表示div里的内容，可以是HTML
click:function(){} 点击后执行的函数
*/

var wScrollToTop={
	scrollTop:0,
	
	right:"",
	top:"",
	content:"置顶",
	autoHide:false,
	fade:false, /*是否淡入淡出,渐渐隐藏渐渐出现*/
	
	Elm:null,
	opacityValue:0,
	TID:0 ,/*存储setInterval返回值*/
	isShow:false /*内部控制，当fade=true时，判断是否已经显示*/
	
}

/*
函数调用入口
options = {top:"10px",right:"20px",content:"字符串或HTML"}
*/
wScrollToTop.init = function(options){
	var div="",Elm,doc;
	if(options!=undefined)
	{
		this.top = options.top || "40%";
		this.right = options.right || "10px";
		this.content = options.content || "置顶";
		this.autoHide = options.autoHide || false;
		this.fade = options.fade || false;
		this.click = options.click;
	}
	else
	{
		this.top =  "40%";
		this.right = "10px";
	}
	Elm = document.createElement("div");
	wScrollToTop.Elm = Elm;
	Elm.id="wScrollToTop";
	Elm.className = "wScrollToTop";
	Elm.style.position="fixed";
	Elm.style.right=this.right;
	Elm.style.top = this.top;
	Elm.style.cursor = "pointer";
	Elm.innerHTML=this.content;
	if(typeof(this.click)=="undefined")
	{
		Elm.onclick=wScrollToTop.onclick;
	}
	else
	{
		Elm.onclick=this.click;
	}
	doc = document.getElementsByTagName("body")[0];
	doc.appendChild(Elm);
	if(this.autoHide)
	{
		Elm.style.display = "none";
		if(this.fade)
		{		
			if(wScrollToTop.Elm.style.opacity==undefined)
			{
				Elm.style.filter="alpha(opacity=0)";
			}
			else
			{
				Elm.style.opacity=0;
			}
		}
		window.onscroll = document.onscroll = function(){
			/*360浏览器不能识别document.documentElement.scrollTop但能识别document.body.scrollTop*/
			/*document.getElementById("lab").innerHTML = (document.documentElement.scrollTop);*/
			wScrollToTop.scrollTop= document.documentElement.scrollTop || document.body.scrollTop;
			if(wScrollToTop.scrollTop>100)
			{
				if(wScrollToTop.fade&&!wScrollToTop.isShow)
				{
					wScrollToTop.fadeIn();
					wScrollToTop.isShow=true;
				}
				else
				{
					Elm.style.display = "block";
				}
			}
			else/*scrollTop==0*/
			{
				if(wScrollToTop.fade)
				{
					/*wScrollToTop.fadeOut();暂时有问题*/
					Elm.style.display = "none";
					wScrollToTop.isShow=false;
					if(wScrollToTop.Elm.style.opacity==undefined)
					{
						Elm.style.filter="alpha(opacity=0)";
					}
					else
					{
						Elm.style.opacity=0;
					}
					
				}
				else
				{
					Elm.style.display = "none";
				}
			}
		}/*end scroll*/
	}/*end if this.autoHide */
} /*end functon wScrollToTop.init*/

wScrollToTop.fadeIn = function(){
	if(wScrollToTop.TID==0)/*在页面滚动时只执行一次*/
	{
		wScrollToTop.Elm.style.display = "block";
		wScrollToTop.TID = setInterval(function(){
			wScrollToTop.opacityValue += 10;
			if(wScrollToTop.Elm.style.opacity==undefined)
			{
				wScrollToTop.Elm.style.filter="alpha(opacity="+wScrollToTop.opacityValue+")";
			}
			else
			{
				wScrollToTop.Elm.style.opacity=wScrollToTop.opacityValue/100;
			}
			if(wScrollToTop.opacityValue>=100)
			{
				clearInterval(wScrollToTop.TID);
				wScrollToTop.TID=0;
				wScrollToTop.opacityValue=0;
			}
		},50);
	}
}/*end function fadeIn*/

wScrollToTop.fadeOut = function(){
	if(wScrollToTop.TID==0)/*在页面滚动时只执行一次*/
	{
		wScrollToTop.Elm.style.display = "none";
		wScrollToTop.TID = setInterval(function(){
			wScrollToTop.opacityValue -= 10;
			if(wScrollToTop.Elm.style.opacity==undefined)
			{
				wScrollToTop.Elm.style.filter="alpha(opacity="+wScrollToTop.opacityValue+")";
			}
			else
			{
				wScrollToTop.Elm.style.opacity=wScrollToTop.opacityValue/100;
			}
			if(wScrollToTop.opacityValue<=0)
			{
				clearInterval(wScrollToTop.TID);
				wScrollToTop.TID=0;
				wScrollToTop.opacityValue;
			}
		},50);
	}
}/*end function fadeIn*/

wScrollToTop.onclick=function(){
	window.scroll(0,0);
}
/*end wScrollToTop */







